#ifndef REDIS_H
#define REDIS_H

#include <hiredis/hiredis.h>
#include <thread>
#include <functional>
#include <string>

using redis_handler = std::function<void(int, std::string)>;

class Redis { 
public:
    Redis();
    ~Redis();

    //连接Redis服务器
    bool connect();
    
    //向Redis指定的通道channel发布消息
    bool publish(int channel, std::string message);
    
    //向Redis指定的通道channel订阅消息
    bool subscribe(int channel);
    
    //取消订阅
    bool unsubscribe(int channel);
    
    //独立线程中接受订阅通道的消息
    void observer_channel_message();
    
    //初始化业务层上报通道消息的回调对象
    void init_notify_handler(redis_handler handler);

private:
    // hiredis同步上下文对象，负责publish和subscribe消息
    redisContext *publish_context_;
    redisContext *subscribe_context_;
    
    //业务层上报通道消息的回调对象
    redis_handler notify_handler_;
};

#endif // !REDIS_H